package com.fjl.filewriter;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class FileOperation {
	public File file;

	public FileOperation(File file){
		this.file = file;
	}
	
	public boolean FileContainStr(String inputString) {
		ArrayList<String> fileStrArrayList = new ArrayList<String>();
		String[] readString = readFrFile().split("\r\n");
		for (int i = 0; i < readString.length; i++) {
			fileStrArrayList.add(readString[i]);
			if (fileStrArrayList.contains(inputString)) {
				return true;
			}
		}
		return false;
	}
	
	public String readFrFile() {
		try {
			byte[] buff = new byte[256];
			FileInputStream fis = new FileInputStream(file);
			int len = fis.read(buff);
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			outputStream.write(buff, 0, len);
			return new String(outputStream.toByteArray());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	//追加到文件尾
	public void write2file(String string) {
		String context = string + "\r\n";
		try {
			FileOutputStream fOutputStream = new FileOutputStream(file, true);//设置为true，可以追加
			fOutputStream.write(context.getBytes());
			fOutputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 读文件的得到文件字节流
	 * @param filePath
	 * @return
	 */
	public byte[] readFileToBytes(String filePath){
		byte[] bytes = null;
		try {
			BufferedInputStream bi = new BufferedInputStream(new FileInputStream(filePath));
			//输入流转字节流
			bytes = new byte[bi.available()];
			bi.read(bytes);
			bi.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bytes;
		
	}
	/**
	 * 把字节流放到本地
	 * 
	 * @param bytes
	 * @param filePath
	 * @throws IOException
	 */
	public static void writeBytesFile(byte[] bytes, String filePath) throws IOException {
		try {
			BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream(filePath));
			bo.write(bytes);
			bo.flush();
			bo.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 输入流转字节流
	 * */
	public static byte[] InputStreamToByte(InputStream is) throws IOException
	{
		ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int ch;
		while ((ch = is.read(buffer)) != -1)
		{
			bytestream.write(buffer, 0, ch);
		}
		byte data[] = bytestream.toByteArray();
		bytestream.close();
		return data;

	}
	
	/**
	 * 读输入流得到文件字节流
	 * 
	 * @param filePath
	 * @return
	 */
	public static byte[] readInputStreamToBytes(InputStream in)
	{
		byte[] bytes = null;
		try
		{
			BufferedInputStream bi = new BufferedInputStream(in);
			// 输入流转字节流
			bytes = new byte[bi.available()];
			bi.read(bytes);
			bi.close();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bytes;

	}
	
	/**
	 * 文件输入流写入到本地
	 * 
	 * @param in
	 * @param filePath
	 */
	public static void writeInputStreamFile(InputStream in,String filePath)
	{
		ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
//		BufferedOutputStream bo;
		byte[] buffer = new byte[1024];
		int ch;
		try
		{
			while ((ch = in.read(buffer)) != -1)
			{
				bytestream.write(buffer, 0, ch);
			}
			byte data[] = bytestream.toByteArray();
			
			// 判断父文件夹是否被创建
			File Fout = new File(filePath);
			if (!Fout.exists()) {
				(new File(Fout.getParent())).mkdirs();
			}
			
			BufferedOutputStream bo = new BufferedOutputStream(
					new FileOutputStream(filePath));
			bo.write(data);
			bo.flush();
			bo.close();
			bytestream.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}


	public void clearFile(String string) {
		String context = string + "\r\n";
		try {
			FileOutputStream fos = new FileOutputStream(file, false);
			fos.write(context.getBytes());
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
